package com.etrans.web.gps.business.platformTask.service;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

import org.springframework.stereotype.Service;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.platformTask.domain.PlatformAnalyzes;
import com.etrans.web.gps.business.platformTask.domain.PlatformVO;
import com.etrans.web.gps.common.MGTemplete;

@Service
public class SendMessageService extends BaseService{
	
	/**
	 * 查询所有的业户
	 * @return 
	 */
/*	public List<HashMap<String, Object>> queryAllEnterList() {
		return selectList("platformMapper.queryAllEnterList",null);
	}*/
	
	/**
	 * 根据平台id查询平台
	 * @return 
	 */
	public PlatformVO queryPlatformById(String platformId) {
		HashMap<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("platformId", Integer.valueOf(platformId));
		return selectOne("platformMapper.queryAllPlatformList",paramMap);
	}
	
	/**
	 * 查询所有的平台
	 * @return 
	 */
	public List<PlatformVO> queryAllPlatformList() {
		return selectList("platformMapper.queryAllPlatformList",null);
	}
	
	/**
	 * 按周查询
	 * @param queryTime 查询时间
	 */
	public List<PlatformAnalyzes> queryPlatAnalyzesWeek(String queryTime){
		HashMap<String,Object> whereMap = new HashMap<String,Object>();
		Pattern pattern = Pattern.compile("^"+queryTime);
		whereMap.put("_id", pattern);
/*		HashMap<String,Object> whereMap_Time = new HashMap<String, Object>();
		whereMap_Time.put("$gte", startTime);	//大于
		whereMap_Time.put("$lte", endTime);		//小于
		whereMap.put("analyse_date", whereMap_Time);*/
		List<PlatformAnalyzes> listAnalyzes = MGTemplete.find("egov", "platform_swap_week",whereMap,PlatformAnalyzes.class,false);
		Collections.sort(listAnalyzes,Collections.reverseOrder(new Comparetores()));
		int order = 1;
		for (int i = 0; i < listAnalyzes.size(); i++) {
			int platScore1 = listAnalyzes.get(i).getScore();
			if (i < listAnalyzes.size()-1) {
				int platScore2 = listAnalyzes.get(i+1).getScore();
				if (platScore1 != platScore2) {
					listAnalyzes.get(i).setRanking(order);
					order++;
				}else {
					listAnalyzes.get(i).setRanking(order);
				}
			}else {
				listAnalyzes.get(i).setRanking(order);
			}
		}
		return  listAnalyzes;
	}
	
	/**
	 * 按月查询
	 * @param queryTime 查询时间
	 * @return
	 */
	public List<PlatformAnalyzes> queryPlatAnalyzesMonth(String queryTime){
		HashMap<String,Object> whereMap = new HashMap<String,Object>();
		Pattern pattern = Pattern.compile("^"+queryTime);
		whereMap.put("_id", pattern);
		List<PlatformAnalyzes> listAnalyzes = MGTemplete.find("egov", "platform_swap_month",whereMap,PlatformAnalyzes.class,true);
		Collections.sort(listAnalyzes,Collections.reverseOrder(new Comparetores()));
		int order = 1;
		for (int i = 0; i < listAnalyzes.size(); i++) {
			int platScore1 = listAnalyzes.get(i).getScore();
			if (i < listAnalyzes.size()-1) {
				int platScore2 = listAnalyzes.get(i+1).getScore();
				if (platScore1 != platScore2) {
					listAnalyzes.get(i).setRanking(order);
					order++;
				}else {
					listAnalyzes.get(i).setRanking(order);
				}
			}else {
				listAnalyzes.get(i).setRanking(order);
			}
		}
		return  listAnalyzes;
	}
	
	/**
	 * 对集合进行排序
	 */
	public static class Comparetores implements Comparator<PlatformAnalyzes>{
		public int compare(PlatformAnalyzes analyzes1, PlatformAnalyzes analyzes2) {
	        int maxs=analyzes1.getScore()-analyzes2.getScore();
	         if(maxs==0){
	        	 int a1=(analyzes1.getTrack_normal()+analyzes1.getException_track_count())<=0?1:(analyzes1.getTrack_normal()+analyzes1.getException_track_count());
	        	 int a2=(analyzes2.getTrack_normal()+analyzes2.getException_track_count())<=0?1:(analyzes2.getTrack_normal()+analyzes2.getException_track_count());
	        	 int maxn = (int)(((double)analyzes1.getException_track_count()/a1)-((double)analyzes2.getException_track_count()/a2));
	        	 if (maxn==0) {
					return analyzes1.getScore()<=analyzes2.getScore()?-1:1;
				} else {
					return maxn;
				}
	         }else{
	 	        return maxs;
	         }
		}		
	}
}
